package com.zzl.finance.data.controller;

import com.zzl.apidoc.annotation.ApidocController;
import com.zzl.apidoc.annotation.ApidocOperation;
import com.zzl.apidoc.annotation.ApidocParam;
import com.zzl.finance.data.dto.*;
import com.zzl.finance.data.service.EnterpriseRelationService;
import com.zzl.general.common.apidoc.annotation.ApidocParamsPage;
import com.zzl.general.common.apidoc.annotation.ApidocResultKeyValR;
import com.zzl.general.common.apidoc.annotation.ApidocReturnPage;
import com.zzl.general.common.apidoc.annotation.ApidocReturnR;
import com.zzl.general.common.apidoc.constants.ResultTypeInR;
import com.zzl.general.common.ctrl.WebBaseCtrl;
import com.zzl.general.common.exception.SysException;
import com.zzl.general.common.utils.LogUtils;
import com.zzl.general.common.utils.PageUtils;
import com.zzl.general.common.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Map;

/**
 * 企业关系表
 *
 * @author sunpp
 * @date 2021-11-30 13:12:59
 */
@ApidocController("4企业关系表")
@RestController
@RequestMapping("data/enterpriserelation")
@Slf4j
public class EnterpriseRelationController extends WebBaseCtrl {

    @Resource
    private EnterpriseRelationService enterpriseRelationService;


    /**
     * 获取 收单企业下拉列表
     * 1。白名单
     * 2.票可流转企业
     */
    @GetMapping("/getAcquirerDropDownList/{ticketId}")
    @ApidocOperation(description = "获取 收单企业下拉列表", operationId = "getEnterpriseGrade/12")
    @ApidocReturnR
    public Result getAcquirerDropDownList(@ApidocParam(name = "ticketId", description = "票据id") @PathVariable("ticketId") String ticketId) {
        return Result.ok().put("data", enterpriseRelationService.getAcquirerDropDownList(ticketId,this.getUser()));
    }


    /**
     * 添加企业 黑名单
     */
    @PutMapping("/saveEnterpriseBlackRelation")
    @ApidocOperation(description = "添加企业 黑或者白名单", operationId = "saveEnterpriseBlackRelation1")
    @ApidocReturnR
    public Result saveEnterpriseBlackRelation(@ApidocParam(name = "添加企业 黑名单", required = true) @Validated @RequestBody BwRelationAddDto bwRelationAddDto) {
        try {
            enterpriseRelationService.saveEnterpriseRelation(bwRelationAddDto, this.getUser());
            return Result.ok();
        } catch (SysException e) {
            LogUtils.logSysException(log, e);
            return Result.error(e.getMsg());
        }
    }

    /**
     * 分页查询白名单数据表数据
     */
    @GetMapping("/listWhitePage")
    @ApidocOperation(description = "供应商白名单列表（只有集团有此菜单权限）", operationId = "listWhitePage2")
    @ApidocReturnPage(genericTypeClass = BwShowDto.class)
    public Result list(@ApidocParamsPage(name = "params", keyName = {"enterpriseName", "enterpriseGrade"}, keyDescription = {"企业名称", "企业评级"})
                       @RequestParam Map<String, Object> params) {
        try {
            PageUtils page = enterpriseRelationService.queryWhitePage(params);
            return Result.ok().put("page", page);
        } catch (SysException e) {
            e.printStackTrace();
            return Result.error(e.getMsg());
        }

    }

    @GetMapping("/quotaSetting/{enterpriseId}")
    @ApidocOperation(description = "供应商白名单-额度配置按钮", operationId = "quotaSetting/enterpriseId")
    @ApidocReturnR(otherkv = {
            @ApidocResultKeyValR(resultType = ResultTypeInR.TYPE_COLLOCTION, key = "datax", valueClass = BwBankShowDto.class)
    })
    public Result quotaSetting(@ApidocParam(name = "enterpriseId", description = "企业id") @PathVariable("enterpriseId") String enterpriseId) {
        try {
            return Result.ok().put("data", enterpriseRelationService.quotaSetting(enterpriseId));
        } catch (SysException e) {
            e.printStackTrace();
            return Result.error(e.getMsg());
        }

    }

    /**
     * 获取 企业评级的 枚举值
     */
    @GetMapping("/getEnterpriseGrade")
    @ApidocOperation(description = "获取 企业评级的 枚举值", operationId = "getEnterpriseGrade")
    @ApidocReturnR
    public Result getEnterpriseGrade() {
        return Result.ok().put("data", enterpriseRelationService.getEnterpriseGrade());
    }


    /**
     * 查询企业黑名单数据
     */
    @GetMapping("/listBlackPage")
    @ApidocOperation(description = "供应商黑名单列表", operationId = "listBlackPage1")
    @ApidocReturnPage(genericTypeClass = BwShowDto.class)
    public Result queryBlackPage(@ApidocParamsPage(name = "params", keyName = {"enterpriseName", "enterpriseGrade"}, keyDescription = {"企业名称", "企业评级"})
                                 @RequestParam Map<String, Object> params) {
        PageUtils page = enterpriseRelationService.queryBlackPage(params);
        return Result.ok().put("page", page);
    }

    /**
     * 修改的企业的评级
     */
    @PutMapping("/updateGrade")
    @ApidocOperation(description = "修改的企业的评级", operationId = "dataEnterRelation4")
    @ApidocReturnR
    public Result update(@ApidocParam(name = "修改的企业的评级", required = true) @RequestBody EnterpriseRelationUpdateDto enterpriseRelation) {
        try {
            enterpriseRelationService.updateEnterpriseRelationById(enterpriseRelation, this.getUser());
            return Result.ok();
        } catch (SysException e) {
            LogUtils.logSysException(log, e);
            return Result.error(e.getMsg());
        }
    }

    /**
     * 解除 供应商关系
     */
    @GetMapping("/deleteSupplierRelation/{id}")
    @ApidocOperation(description = "供应商黑名单-解除黑名单", operationId = "deleteSupplierRelation1")
    @ApidocReturnR
    public Result deleteSupplierRelation(@ApidocParam(name = "id", description = "关系的 id,行数据的id", required = true) @PathVariable("id") String id) {
        enterpriseRelationService.deleteSupplierRelation(id);
        return Result.ok();
    }

    @GetMapping("/getInvitationLink")
    @ApidocOperation(description = "供应商白名单-邀请链接", operationId = "getInvitationLink1")
    @ApidocReturnR
    public Result getInvitationLink() {
        return Result.ok().put("data", enterpriseRelationService.getInvitationLink(this.request, this.getUser()));
    }

    /**
     * 设置下级供应商为 黑名单 / 白名单
     */
    @PutMapping("/update")
    @ApidocOperation(description = "添加至黑名单/白名单（行对应的按钮操作）", operationId = "dataEnterRelation41")
    @ApidocReturnR
    public Result updateBw(@ApidocParam(name = "设置黑白名单实体", required = true) @RequestBody SetBlackOrWhiteDto setBlackOrWhiteDto) {
        try {
            return Result.ok().message(enterpriseRelationService.updateBw(setBlackOrWhiteDto, this.getUser()));
        } catch (SysException e) {
            LogUtils.logSysException(log, e);
            return Result.error(e.getMsg());
        }
    }


    /**
     * 删除
     */
    @DeleteMapping("/delete")
    @ApidocOperation(description = "删除企业关系表", operationId = "dataEnterRelation5/z1")
    @ApidocReturnR
    public Result delete(@ApidocParam(name = "删除的企业关系表对象id集合", required = true) @RequestBody Integer[] ids) {
        enterpriseRelationService.removeByIds(Arrays.asList(ids));
        return Result.ok();
    }


    /**
     * 供应商 相关接口
     * <p>
     * 添加供应商 的下级供应商
     */
    @GetMapping("/getSupplierRelationPage")
    @ApidocOperation(description = "供应商--> 供应商管理--> 分页查询供应商管理", operationId = "getSupplierRelationPage")
    @ApidocReturnPage(genericTypeClass = SupplierRelationShowDto.class)
    public Result getSupplierRelationPage(@ApidocParamsPage(name = "params", keyName = {"enterpriseName"}, keyDescription = {"企业名称"})
                                          @RequestParam Map<String, Object> params) {
        return Result.ok().put("page", enterpriseRelationService.getSupplierRelationPage(params, this.getUser()));
    }

    /**
     * 添加供应商 的下级供应商
     */
    @GetMapping("/addSubordinate")
    @ApidocOperation(description = "供应商--> 供应商管理--> 添加供应商", operationId = "addSubordinate")
    @ApidocReturnR
    public Result addSubordinate(@RequestParam("id") String id) {
        try {
            enterpriseRelationService.addSubordinate(id, this.getUser());
            return Result.ok();
        } catch (SysException e) {
            LogUtils.logSysException(log, e);
            return Result.error(e.getMsg());
        }

    }


}
